const script = document.createElement('script')

const serverHost = '${serverHost}';
script.onload = () => {
    const socket = io(serverHost);
    const wLog = window.console.log
    const wErr = window.console.error
    let user = '';
    function getUser() {
        if (user) return user;
        if (abp) {
            if (abp.utils) {
                try {
                    let token = abp.utils.getCookieValue('Abp.AuthToken');
                    if (token) {
                        let t = JSON.parse(atob(token.split('.')[1].replace(/_/g, '+')))
                        if (t) {
                            user = t['http://auth.52abp.com/identity/claims/UserName']
                            setTimeout(() => {
                                user = ''
                            }, 1000 * 60);
                        }
                    }
                } catch (error) {
                    return ''
                }
            }
        }
        return user;
    }

    window.console.log = function (...a) {
        wLog(...a);
        socket.emit('log', getUser(), ...a)
    }
    window.console.error = function (...a) {
        wErr(...a);
        socket.emit('err', getUser(), ...a)
    }
    window.ctrl_c = function (...a) {
        socket.emit('ctrl_c', ...a);
    }
    socket.on('connect', () => {
        wLog('日志连接已就绪')
    })
}
script.async = true;
script.src = serverHost + '/socket.io/socket.io.js'
document.head.appendChild(script)
